package service

import (
    "gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
	"encoding/json"
)

type ProductCustomFieldListColumnService interface {
	ListProductCustomFieldListColumn(ctx context.Context, req *api.ListProductCustomFieldListColumnRequest) (*api.ListProductCustomFieldListColumnResponse, error)
	GetProductCustomFieldListColumn(ctx context.Context, req *api.ProductCustomFieldListColumnData) (*api.ProductCustomFieldListColumnData, error)
	AddProductCustomFieldListColumn(ctx context.Context, req *api.ProductCustomFieldListColumnData) error
	EditProductCustomFieldListColumn(ctx context.Context, req *api.ProductCustomFieldListColumnData) error
	DeleteProductCustomFieldListColumn(ctx context.Context, id int64) (err error)
	BatchDeleteProductCustomFieldListColumn(ctx context.Context, req *api.BatchDeleteProductCustomFieldListColumnRequest) (err error)
}

type somsService struct {
	productCustomFieldListColumnRepo repo.ProductCustomFieldListColumnRepo
}

func (s *somsService) ListProductCustomFieldListColumn(ctx context.Context, req *api.ListProductCustomFieldListColumnRequest) (*api.ListProductCustomFieldListColumnResponse, error) {
	dbDatas, count, err := s.productCustomFieldListColumnRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.ProductCustomFieldListColumnDBDataParam{
	    ProductCustomFieldListColumnDBData: repo.ProductCustomFieldListColumnDBData {
            ProductCustomFieldId: req.Data.ProductCustomFieldId,
			ColumnName: req.Data.ColumnName,
			MasterFlag: req.Data.MasterFlag,
			
	    },
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.ProductCustomFieldListColumnData
	if dbDatas != nil {
		result = make([]*api.ProductCustomFieldListColumnData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.ProductCustomFieldListColumnData{
				Id:                  data.Id,
                OrganizationId:      data.OrganizationId,
                CreatedAt:           util.FormatDateTime(data.CreatedAt),
                UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
                CreatedUserId:       data.CreatedUserId,
                UpdatedUserId:       data.UpdatedUserId,
                UpdatedUserNickname: data.UpdatedUserNickname,
                CreatedUserNickname: data.CreatedUserNickname,
				ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
			}
		}
	}
	return &api.ListProductCustomFieldListColumnResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetProductCustomFieldListColumn(ctx context.Context, req *api.ProductCustomFieldListColumnData) (*api.ProductCustomFieldListColumnData, error) {
	data, err := s.productCustomFieldListColumnRepo.Get(ctx, &repo.ProductCustomFieldListColumnDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: req.Id,
        			},
        		},
		})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.ProductCustomFieldListColumnData{
		Id:                  data.Id,
        OrganizationId:      data.OrganizationId,
        CreatedAt:           util.FormatDateTime(data.CreatedAt),
        UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
        CreatedUserId:       data.CreatedUserId,
        UpdatedUserId:       data.UpdatedUserId,
        UpdatedUserNickname: data.UpdatedUserNickname,
        CreatedUserNickname: data.CreatedUserNickname,
		ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
	}
	return resp, nil
}

func (s *somsService) AddProductCustomFieldListColumn(ctx context.Context, data *api.ProductCustomFieldListColumnData) error {
	repoData := &repo.ProductCustomFieldListColumnDBData{
		ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
	}
	_, err := s.productCustomFieldListColumnRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditProductCustomFieldListColumn(ctx context.Context, data *api.ProductCustomFieldListColumnData) error {
	repoData := &repo.ProductCustomFieldListColumnDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: data.Id,
        			},
        		},
		ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
	}
	return handler.HandleError(s.productCustomFieldListColumnRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteProductCustomFieldListColumn(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.productCustomFieldListColumnRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteProductCustomFieldListColumn(ctx context.Context, req *api.BatchDeleteProductCustomFieldListColumnRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
